# src/models/schemas.py
from __future__ import annotations
from typing import Optional, List, Any
from pydantic import BaseModel, Field, EmailStr, conint
from datetime import date, datetime

# Patients 
class EmergencyContact(BaseModel):
    name: str
    phone: str
    relation: Optional[str] = None

class PatientCreate(BaseModel):
    first_name: str = Field(..., min_length=1)
    last_name: str = Field(..., min_length=1)
    date_of_birth: date
    gender: Optional[str] = None
    email: Optional[EmailStr] = None
    phone: Optional[str] = None
    address: Optional[str] = None
    emergency_contact: Optional[EmergencyContact] = None

class PatientUpdate(BaseModel):
    first_name: Optional[str]
    last_name: Optional[str]
    date_of_birth: Optional[date]
    gender: Optional[str]
    email: Optional[EmailStr]
    phone: Optional[str]
    address: Optional[str]
    emergency_contact: Optional[EmergencyContact]
    is_active: Optional[bool]

class PatientOut(BaseModel):
    patient_id: int
    first_name: str
    last_name: str
    date_of_birth: date
    gender: Optional[str]
    email: Optional[EmailStr]
    phone: Optional[str]
    address: Optional[str]
    registration_date: datetime
    emergency_contact: Optional[Any]
    is_active: bool

# Appointments
class AppointmentCreate(BaseModel):
    patient_id: int
    staff_id: int
    department_id: Optional[int] = None
    appointment_datetime: datetime
    duration_minutes: conint(gt=0)
    reason: Optional[str] = None
    status: Optional[str] = "scheduled"

class AppointmentUpdate(BaseModel):
    appointment_datetime: Optional[datetime]
    duration_minutes: Optional[conint(gt=0)]
    reason: Optional[str]
    status: Optional[str]
    staff_id: Optional[int]
    department_id: Optional[int]

class AppointmentOut(BaseModel):
    appointment_id: int
    patient_id: int
    staff_id: int
    department_id: Optional[int]
    appointment_datetime: datetime
    duration_minutes: int
    reason: Optional[str]
    status: str
    created_at: datetime

#  Medical Records 
class MedicalRecordCreate(BaseModel):
    appointment_id: int
    patient_id: int
    diagnosis: Optional[str]
    symptoms: Optional[str]
    observations: Optional[str]

class MedicalRecordUpdate(BaseModel):
    diagnosis: Optional[str]
    symptoms: Optional[str]
    observations: Optional[str]

class MedicalRecordOut(BaseModel):
    record_id: int
    appointment_id: int
    patient_id: int
    diagnosis: Optional[str]
    symptoms: Optional[str]
    observations: Optional[str]
    created_at: datetime
    updated_at: datetime

# Prescriptions 
class PrescriptionItemSchema(BaseModel):
    medication_id: int
    dosage: Optional[str]
    frequency: Optional[str]
    duration_days: Optional[int]

class PrescriptionCreate(BaseModel):
    appointment_id: int
    prescribed_by: int
    notes: Optional[str]
    items: List[PrescriptionItemSchema] = []

class PrescriptionOut(BaseModel):
    prescription_id: int
    appointment_id: int
    prescribed_by: int
    issued_at: datetime
    notes: Optional[str]
    items: Optional[List[Any]] = []

#  Payments / Invoices
class InvoiceItemSchema(BaseModel):
    description: str
    amount: float

class InvoiceCreate(BaseModel):
    patient_id: int
    appointment_id: Optional[int]
    items: List[InvoiceItemSchema] = []

class InvoicePaySchema(BaseModel):
    amount: float = Field(..., gt=0)
